package com.xiaowu.xblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaowu.xblog.repository.BaseRepository;
import com.xiaowu.xblog.service.BaseService;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

public abstract class BaseServiceImpl<R extends BaseRepository<T>, T> implements BaseService<T> {

    protected final R repository;


    protected BaseServiceImpl(R repository) {
        this.repository = repository;
    }

    @Override
    public boolean create(T entity) {
        return repository.save(entity);
    }

    @Override
    public boolean createBatch(Collection<T> entityList) {
        return repository.saveBatch(entityList);
    }

    @Override
    public boolean createOrUpdate(T entity) {
        return repository.saveOrUpdate(entity);
    }

    @Override
    public boolean createOrUpdateBatch(Collection<T> entityList) {
        return repository.saveOrUpdateBatch(entityList);
    }

    @Override
    public boolean deleteById(Serializable id) {
        return repository.removeById(id);
    }

    @Override
    public boolean deleteByIds(Collection<? extends Serializable> idList) {
        return repository.removeByIds(idList);
    }

    @Override
    public boolean delete(Wrapper<T> queryWrapper) {
        return repository.remove(queryWrapper);
    }

    @Override
    public boolean updateById(T entity) {
        return repository.updateById(entity);
    }

    @Override
    public boolean updateBatchById(Collection<T> entityList) {
        return repository.updateBatchById(entityList);
    }

    @Override
    public boolean update(T entity, Wrapper<T> updateWrapper) {
        return repository.update(entity, updateWrapper);
    }

    @Override
    public T findById(Serializable id) {
        return repository.getById(id);
    }

    @Override
    public List<T> findByIds(Collection<? extends Serializable> idList) {
        return repository.listByIds(idList);
    }

    @Override
    public List<T> findAll() {
        return repository.list();
    }

    @Override
    public List<T> findList(Wrapper<T> queryWrapper) {
        return repository.list(queryWrapper);
    }

    @Override
    public IPage<T> findPage(IPage<T> page) {
        return repository.page(page);
    }

    @Override
    public IPage<T> findPage(IPage<T> page, Wrapper<T> queryWrapper) {
        return repository.page(page, queryWrapper);
    }

    @Override
    public long count() {
        return repository.count();
    }

    @Override
    public long count(Wrapper<T> queryWrapper) {
        return repository.count(queryWrapper);
    }

    @Override
    public boolean exists(Wrapper<T> queryWrapper) {
        return repository.count(queryWrapper) > 0;
    }
}
